<?php
	require_once( ROOT_DIR."config.inc.php" );
//	require_once( ROOT_DIR."uc_client/client.php" );

	$errmsg = GetMessageStack();
	
	$redirect = GET('redir');
			
	$email = POST('email');
		
	$nick = POST('nick');
		
	$password = POST('pass');
	
	$rePassword = POST('repass');
		
	$intrID = htmlspecialchars( GET('intrID') );
		
	//$province = trim($_POST['province']);
	//$city = trim($_POST['city']);

	$vcode = edp_db_input( POST('vcode') );
	
	function ProcessRegister()
	{
		global $errmsg, $email, $nick, $password, $rePassword, $intrID, $vcode, $page_base;
		/*
		 * 服务器端验证
		 */
		if( $vcode != edp_getsession( 'vcode' ) )
		{
			$errmsg->add( $page_base, '验证码无效！' );
			return;
		}
		
		if( $password != $rePassword )
		{
			$errmsg->add( $page_base, '密码输入不一致！');
			return;
		}
		
							
		if( !IsParaOK() )
			return;

		/**********************************************************
		 * >> Important! << 
		 * All date from the client must be filtered by
		 * the edp_db_input before sending to the DAO
		 **********************************************************/
		$nick = edp_db_input( $nick );
		$email = edp_db_input( $email );
		//$province = edp_db_input( $province );
		//$city = edp_db_input( $city );	
		
		$userDao = GetUserDAO();
		if( $userDao->IsEmailExist( $email ) )
		{
			$errmsg->add( $page_base, '该Email地址已经存在！' );
			return;
		}
		if(	$userDao->IsNickExist($nick))
		{
			$errmsg->add( $page_base, '该昵称已经存在！' );
			return;
		}
		
		/**********************************************************
		 *	All validations have been proved valid, then add the 
		 *  new user
		 **********************************************************/
		//$logo_path = IMAGE_DIR."avatar.gif";
		$logo_path = '';
		$introduction = "";
		$register_date = GetCurrentDate( 'Y-n-j' );
		$last_login = $register_date;
		$name = "";
		$address = "";
		$phone = "";
		$zip = "";
		$pwd = md5( $password );
		
		$id = $userDao->AddNewUser(	$nick, $logo_path, $introduction, 
									$register_date, $last_login, $email,
									$pwd, $name, $address, $zip, $phone	);
									
		/***************************************************
		 * add by bo at 2010.07.26
		 * 先清除cookie，存在用户登录状态下再次注册新帐号情况
		 *************************************************/							
		logoutUser();

        //now need mail activation, so does not automatically login
		//LoginAsUser($id, $nick, false);
		
		//Add user register event and contribution
		$eventDispatcher = GetEventDispatcher();
		$eventDispatcher->Notify( $id, REGISTER );

        $activationUrl = edp_href_link( PAGENAME_ACTIVATE, "uid=$id&username=$nick&stamp=".base64_encode($password)."&c=".EdpGetActivationCode($id, $nick) );
		$mail=GetMailer();
		//Add user inviting event and contribution
		if($intrID>'0'&&($inviter=$userDao->GetUserByID($intrID))!=false)
		{
		
		    $activationUrl = edp_href_link( PAGENAME_ACTIVATE, "uid=$id&iid=$intrID&username=$nick&c=".EdpGetActivationCode($id, $nick) );
			$para['uid']=$id;
			//$eventDispatcher->Notify( $inviter['id'], INVITE_FRIEND_SUCCESS, $para ); 移到激活成功
			//发给邀请人 移到激活成功
			//$mail->SendInviteSuccessMail($inviter['email'], $inviter['nick'], $nick, $id);
			//发给被邀请人
			$mail->SendRegisterWithInviterMail($email, $nick, $inviter['nick'], $inviter['id'], $activationUrl);
			
			//互相加为好友
		
			$friendDAO = GetFriendDAO();
			if($friendDAO->AddFriend($intrID, $id))
			{
				$para['uid']=$id;
				$eventDispatcher->Notify( $intrID, ADD_FRIEND, $para );
			}

			if($friendDAO->AddFriend($id, $intrID))
			{
				$para['uid']=$intrID;
				$eventDispatcher->Notify( $id, ADD_FRIEND, $para );
			}			
			
		}
		else{
		
			$mail->SendRegisterMail($email, $nick, $activationUrl);
			
		}	

		//add user to ucenter for other system to login
//		if( !uc_get_user($nick) ){
//			$user_id = uc_user_register( $nick, $password, $email );

//			//do login to ucenter
//			$ucsynlogin = uc_user_synlogin($user_id);
//			echo $ucsynlogin;
//        }

        //for generate activation url again
        edp_setsession( "register_userid", $id );
        edp_setsession( "register_password", $password );

		TransferTo('register_success');
		
	}

	/**********************************************************
	 * to use function instead of directly process because
	 * it's convinient to break out from the deep nested statement
	 * from the function
	 **********************************************************/

	if(isset($_POST['action']))
	{
		$action=trim($_POST['action']);
		if($action=='register')
			ProcessRegister();
	}
	
?>
